package com.ruoyi.monitor.service.impl;

import java.text.DecimalFormat;
import java.util.*;

import com.ruoyi.base.domain.BusWaterSegment;
import com.ruoyi.base.domain.BussinessItemVo;
import com.ruoyi.base.service.IBusWaterSegmentService;
import com.ruoyi.base.service.PublicToolsService;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.monitor.domain.*;
import com.ruoyi.monitor.mapper.BusWaterIntegrationmonitordataMapper;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.monitor.mapper.BusWaterIntegrationevaluatedataMapper;
import com.ruoyi.monitor.service.IBusWaterIntegrationevaluatedataService;
import org.springframework.util.CollectionUtils;

/**
 * 手工站综合断面评价（月）Service业务层处理
 * 
 * @author maliang
 * @date 2025-08-16
 */
@Service
public class BusWaterIntegrationevaluatedataServiceImpl implements IBusWaterIntegrationevaluatedataService 
{
    @Autowired
    private BusWaterIntegrationevaluatedataMapper busWaterIntegrationevaluatedataMapper;
    @Autowired
    private PublicToolsService publicToolsService;
    @Autowired
    private ISysDictDataService sysDictDataService;
    @Autowired
    private IBusWaterSegmentService busWaterSegmentService;
    @Autowired
    private BusWaterIntegrationmonitordataMapper busWaterIntegrationmonitordataMapper;

    /**
     * 查询手工站综合断面评价（月）
     * 
     * @param segmentid 手工站综合断面评价（月）主键
     * @return 手工站综合断面评价（月）
     */
    @Override
    public BusWaterIntegrationevaluatedata selectBusWaterIntegrationevaluatedataBySegmentid(String segmentid)
    {
        return busWaterIntegrationevaluatedataMapper.selectBusWaterIntegrationevaluatedataBySegmentid(segmentid);
    }

    /**
     * 查询手工站综合断面评价（月）列表
     * 
     * @param busWaterIntegrationevaluatedata 手工站综合断面评价（月）
     * @return 手工站综合断面评价（月）
     */
    @Override
    public List<BusWaterIntegrationevaluatedata> selectBusWaterIntegrationevaluatedataList(BusWaterIntegrationevaluatedata busWaterIntegrationevaluatedata)
    {
        return busWaterIntegrationevaluatedataMapper.selectBusWaterIntegrationevaluatedataList(busWaterIntegrationevaluatedata);
    }

    /**
     * 新增手工站综合断面评价（月）
     * 
     * @param busWaterIntegrationevaluatedata 手工站综合断面评价（月）
     * @return 结果
     */
    @Override
    public int insertBusWaterIntegrationevaluatedata(BusWaterIntegrationevaluatedata busWaterIntegrationevaluatedata)
    {
        return busWaterIntegrationevaluatedataMapper.insertBusWaterIntegrationevaluatedata(busWaterIntegrationevaluatedata);
    }

    /**
     * 修改手工站综合断面评价（月）
     * 
     * @param busWaterIntegrationevaluatedata 手工站综合断面评价（月）
     * @return 结果
     */
    @Override
    public int updateBusWaterIntegrationevaluatedata(BusWaterIntegrationevaluatedata busWaterIntegrationevaluatedata)
    {
        return busWaterIntegrationevaluatedataMapper.updateBusWaterIntegrationevaluatedata(busWaterIntegrationevaluatedata);
    }

    /**
     * 批量删除手工站综合断面评价（月）
     * 
     * @param segmentids 需要删除的手工站综合断面评价（月）主键
     * @return 结果
     */
    @Override
    public int deleteBusWaterIntegrationevaluatedataBySegmentids(String[] segmentids)
    {
        return busWaterIntegrationevaluatedataMapper.deleteBusWaterIntegrationevaluatedataBySegmentids(segmentids);
    }

    /**
     * 删除手工站综合断面评价（月）信息
     * 
     * @param segmentid 手工站综合断面评价（月）主键
     * @return 结果
     */
    @Override
    public int deleteBusWaterIntegrationevaluatedataBySegmentid(String segmentid)
    {
        return busWaterIntegrationevaluatedataMapper.deleteBusWaterIntegrationevaluatedataBySegmentid(segmentid);
    }

    @Override
    public List<Map> qualityLevelStatis(String id, String startTime, String endTime) {
        List<Map> dataMapList = new ArrayList<>();

        SysDictData dictParam = new SysDictData();
        dictParam.setDictType("water_quality_level");
        List<SysDictData> sysDictDataList = sysDictDataService.selectDictDataList(dictParam);
        Date startDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",startTime+"-01 00:00:00");
        Date endDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",endTime+"-31 23:59:59");
        List<Map> mapList = busWaterIntegrationevaluatedataMapper.qualityLevelStatis(id,startDate,endDate);

        for (SysDictData sysDictData : sysDictDataList) {
            Map dataMap = new HashMap<>();
            String value = "0";
            if(!CollectionUtils.isEmpty(mapList)){
                for (Map map : mapList) {
                    String qualityLevelName = StringUtils.getString(map.get("qualityLevelName"));
                    if(qualityLevelName.equals(sysDictData.getDictLabel())){
                        value = StringUtils.getString(map.get("qualityLevelCount"));
                        break;
                    }
                }
            }

            dataMap.put(sysDictData.getDictLabel(),value);
            dataMapList.add(dataMap);
        }

        return dataMapList;
    }

    @Override
    public Map statisItemCount(String id, String startTime, String endTime) {
        Map<String,List<String>> itemDataList = new HashMap();
        BussinessItemVo vo = new BussinessItemVo();
        vo.setBussinessCategoryCode("MSV");
        List<Map<String,Object>> itemList=publicToolsService.bussinessItemData(vo);
        Date startDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",startTime+"-01 00:00:00");
        Date endDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",endTime+"-31 23:59:59");

        List<Map> statisList = busWaterIntegrationevaluatedataMapper.statisItemCount(id,startDate,endDate);

        while(!startDate.after(endDate)){
            String dayTime = DateUtils.parseDateToStr("yyyy-MM",startDate);
            if(itemDataList.containsKey("dateList")){
                List<String> dateList = itemDataList.get("dateList");
                dateList.add(dayTime);
                itemDataList.put("dateList",dateList);
            }else{
                List<String> dateList = new ArrayList<>();
                dateList.add(dayTime);
                itemDataList.put("dateList",dateList);
            }
            for (Map<String, Object> itemMap : itemList) {
                String itemName = StringUtils.getString(itemMap.get("itemName"));
                Map itemDataMap = new HashMap();
                itemDataMap.put("day",dayTime);
                itemDataMap.put("item",itemName);
                int count = 0;
                for (Map statisMap : statisList) {
                    String statisDay = StringUtils.getString(statisMap.get("dataDay"));
                    String statisItem = StringUtils.getString(statisMap.get("itemName"));
                    String dataCount = StringUtils.getString(statisMap.get("dataCount"));
                    if(statisDay.equals(dayTime) && itemName.equals(statisItem)){
                        count = Integer.valueOf(dataCount);
                        break;
                    }
                }
                if(itemDataList.containsKey(itemName)){
                    List<String> dateList = itemDataList.get(itemName);
                    dateList.add(count+"");
                    itemDataList.put(itemName,dateList);
                }else{
                    List<String> dateList = new ArrayList<>();
                    dateList.add(count+"");
                    itemDataList.put(itemName,dateList);
                }
            }
            startDate = DateUtils.addMonths(startDate,1);
        }
        return itemDataList;
    }

    @Override
    public Map trendStatis(String id, String startTime, String endTime,String items) {
        List<String> queryItemList = new ArrayList<>();
        if(StringUtils.isNotEmpty(items)){
            queryItemList = Arrays.asList(items.split(","));
        }

        Map<String,List<String>> itemDataList = new HashMap<>();
        BussinessItemVo vo = new BussinessItemVo();
        vo.setBussinessCategoryCode("MSV");
        List<Map<String,Object>> itemList=publicToolsService.bussinessItemData(vo);
        Date startDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",startTime+"-01 00:00:00");
        Date endDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",endTime+"-31 23:59:59");

        List<Map> statisList = busWaterIntegrationevaluatedataMapper.trendStatis(id,startDate,endDate);

        while(!startDate.after(endDate)){
            String dayTime = DateUtils.parseDateToStr("yyyy-MM",startDate);
            if(itemDataList.containsKey("dateList")){
                List<String> dateList = itemDataList.get("dateList");
                dateList.add(dayTime);
                itemDataList.put("dateList",dateList);
            }else{
                List<String> dateList = new ArrayList<>();
                dateList.add(dayTime);
                itemDataList.put("dateList",dateList);
            }
            for (Map<String, Object> itemMap : itemList) {
                String itemName = StringUtils.getString(itemMap.get("itemName"));
                if(!queryItemList.contains(itemName)){
                    continue;
                }
                Map itemDataMap = new HashMap();
                itemDataMap.put("dataTime",dayTime);
                itemDataMap.put("item",itemName);
                String effectiveValue = "0";
                for (Map statisMap : statisList) {
                    String statisDay = StringUtils.getString(statisMap.get("dataDay"));
                    String statisItem = StringUtils.getString(statisMap.get("itemName"));
                    String effective = StringUtils.getString(statisMap.get("effectiveValue"));
                    if(statisDay.equals(dayTime) && itemName.equals(statisItem)){
                        if(StringUtils.isNotEmpty(effective)){
                            effectiveValue = effective;
                        }
                        break;
                    }
                }
                if(itemDataList.containsKey(itemName)){
                    List<String> dataList = itemDataList.get(itemName);
                    dataList.add(effectiveValue);
                    itemDataList.put(itemName,dataList);
                }else{
                    List<String> dataList = new ArrayList<>();
                    dataList.add(effectiveValue);
                    itemDataList.put(itemName,dataList);
                }
            }
            startDate = DateUtils.addMonths(startDate,1);
        }
        return itemDataList;
    }

    @Override
    public List<Map> statisCalendar(String id, String startYear,String endYear) {
        Date startDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",startYear+"-01-01 00:00:00");
        Date endDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",endYear+"-12-31 00:00:00");
        BusWaterIntegrationevaluatedata param = new BusWaterIntegrationevaluatedata();
        param.setSegmentid(id);
        param.setStartTime(startDate);
        param.setEndTime(endDate);
        List<BusWaterIntegrationevaluatedata> dataList = selectBusWaterIntegrationevaluatedataList(param);

        Map<String,Map> map = new LinkedHashMap<>();
        List<Map> dataMapList = new ArrayList<>();
        while(!endDate.before(startDate)){
            String year = DateUtils.parseDateToStr("yyyy",endDate);
            String month = DateUtils.parseDateToStr("MM",endDate);
            String day = DateUtils.parseDateToStr("yyyy-MM",endDate);

            Map dataMap = new HashMap();
            if(map.containsKey(year)){
                dataMap = map.get(year);
                dataMap.put("year",year);
            }
            String isOverStandard = "1";
            for (BusWaterIntegrationevaluatedata busWaterIntegrationevaluatedata : dataList) {
                String dataDay = DateUtils.parseDateToStr("yyyy-MM",busWaterIntegrationevaluatedata.getSampledate());
                if(dataDay.equals(day)){
                    isOverStandard = busWaterIntegrationevaluatedata.getIsoverstandard();
                    break;
                }
            }
            dataMap.put(month,isOverStandard);
            map.put(year,dataMap);
            endDate = DateUtils.addMonths(endDate,-1);
        }
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            dataMapList.add(map.get(key));
        }
        return dataMapList;
    }

    @Override
    public Map trendQuality(String id, String startTime, String endTime) {
        Map<String,List<String>> itemDataList = new HashMap();

        SysDictData sysDictDataParam = new SysDictData();
        sysDictDataParam.setDictType("water_quality_level");
        List<SysDictData> sysDictDataList = sysDictDataService.selectDictDataList(sysDictDataParam);
        List<String> levelList = new ArrayList<>();
        sysDictDataList.sort(Comparator.comparing(SysDictData::getDictSort).reversed());
        for (SysDictData sysDictData : sysDictDataList) {
            levelList.add(sysDictData.getDictLabel());
        }
        itemDataList.put("levelList",levelList);

        Date startDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",startTime+"-01 00:00:00");
        Date endDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",endTime + "-31 23:59:59");

        BusWaterIntegrationevaluatedata param = new BusWaterIntegrationevaluatedata();
        param.setSegmentid(id);
        param.setStartTime(startDate);
        param.setEndTime(endDate);
        List<BusWaterIntegrationevaluatedata> statisList = selectBusWaterIntegrationevaluatedataList(param);

        while(!startDate.after(endDate)){
            String dayTime = DateUtils.parseDateToStr("yyyy-MM",startDate);
            if(itemDataList.containsKey("dateList")){
                List<String> dateList = itemDataList.get("dateList");
                dateList.add(dayTime);
                itemDataList.put("dateList",dateList);
            }else{
                List<String> dateList = new ArrayList<>();
                dateList.add(dayTime);
                itemDataList.put("dateList",dateList);
            }
            String itemName = "level";

            Map itemDataMap = new HashMap();
            itemDataMap.put("dataTime",dayTime);
            itemDataMap.put("item",itemName);
            String level = "无类别";

            for (BusWaterIntegrationevaluatedata statisMap : statisList) {
                String statisDay = DateUtils.parseDateToStr("yyyy-MM",statisMap.getSampledate());
                if(statisDay.equals(dayTime)){
                    if(StringUtils.isNotEmpty(statisMap.getQualitylevelname())){
                        level = statisMap.getQualitylevelname();
                    }
                    break;
                }
            }
            if(itemDataList.containsKey(itemName)){
                List<String> dataList = itemDataList.get(itemName);
                dataList.add(level);
                itemDataList.put(itemName,dataList);
            }else{
                List<String> dataList = new ArrayList<>();
                dataList.add(level);
                itemDataList.put(itemName,dataList);
            }
            startDate = DateUtils.addMonths(startDate,1);
        }
        return itemDataList;
    }


    @Override
    public Map overview(String id) {
        Map map = new HashMap();
        BusWaterSegment busWaterSegment = busWaterSegmentService.selectBusWaterSegmentByID(id);
        map.put("basinName",busWaterSegment.getBasinname());
        map.put("riverName",busWaterSegment.getRivername());
        map.put("county",busWaterSegment.getCounty());
        map.put("segmentCategory",busWaterSegment.getSegmentcategory());
        map.put("controlLevel",busWaterSegment.getControllevel());
        map.put("targetLevelName",busWaterSegment.getTargetlevelname());
        map.put("longitude",busWaterSegment.getLongitude());
        map.put("latitude",busWaterSegment.getLatitude());

        BusWaterIntegrationevaluatedata integrationevaluatedataParam = new BusWaterIntegrationevaluatedata();
        integrationevaluatedataParam.setSegmentid(id);
        List<BusWaterIntegrationevaluatedata> integrationevaluatedataList = busWaterIntegrationevaluatedataMapper.selectLastData(integrationevaluatedataParam);
        if(!CollectionUtils.isEmpty(integrationevaluatedataList)){
            BusWaterIntegrationevaluatedata busWaterIntegrationevaluatedata = integrationevaluatedataList.get(0);
            map.put("sampleTime", DateUtils.parseDateToStr("yyyy-MM-dd HH",busWaterIntegrationevaluatedata.getSampledate()));
            map.put("waterQuality",busWaterIntegrationevaluatedata.getWaterquality());
            map.put("segmentIndex",busWaterIntegrationevaluatedata.getSegmentindex());
            map.put("isoverstandard",busWaterIntegrationevaluatedata.getIsoverstandard());
            map.put("qualitylevelname",busWaterIntegrationevaluatedata.getQualitylevelname());

            //获取检测因子配置
            List<Map> itemDataList = new ArrayList<>();
            List<Map> overstandardList = new ArrayList<>();
            BussinessItemVo vo = new BussinessItemVo();
            vo.setBussinessCategoryCode("MSV");
            DecimalFormat df=new DecimalFormat("####.####");
            List<Map<String,Object>> itemList=publicToolsService.bussinessItemData(vo);
            for (Map<String, Object> stringObjectMap : itemList) {
                BusWaterIntegrationmonitordata param = new BusWaterIntegrationmonitordata();
                param.setSegmentid(busWaterIntegrationevaluatedata.getSegmentid());
                param.setItemcode(StringUtils.getString(stringObjectMap.get("itemCode")));
                param.setSampledate(busWaterIntegrationevaluatedata.getSampledate());
                List<BusWaterIntegrationmonitordata> dataList = busWaterIntegrationmonitordataMapper.selectBusWaterIntegrationmonitordataList(param);
                Map tmpMap = new HashMap();
                tmpMap.put("name",StringUtils.getString(stringObjectMap.get("itemName")));
                if(!CollectionUtils.isEmpty(dataList)){
                    BusWaterIntegrationmonitordata data = dataList.get(0);
                    if(StringUtils.isNotEmpty(data.getEffectivevalue()) && Double.parseDouble(data.getEffectivevalue()) > 0){
                        tmpMap.put("value",df.format(Double.parseDouble(data.getEffectivevalue())));
                    }else{
                        tmpMap.put("value",data.getEffectivevalue());
                    }

                    tmpMap.put("isOverStandard",data.getIsoverstandard());
                    if("1".equals(data.getIsoverstandard())){
                        overstandardList.add(tmpMap);
                    }
                }else{
                    tmpMap.put("value","");
                    tmpMap.put("isOverStandard","");
                }
                itemDataList.add(tmpMap);
            }
            map.put("itemDataList",itemDataList);
            map.put("overstandardList",overstandardList);
        }else{
            map.put("sampleTime", "");
            map.put("waterQuality","");
            map.put("segmentIndex","");
            //获取检测因子配置
            List<Map> itemDataList = new ArrayList<>();
            List<Map> overstandardList = new ArrayList<>();
            BussinessItemVo vo = new BussinessItemVo();
            vo.setBussinessCategoryCode("Auto");
            List<Map<String,Object>> itemList=publicToolsService.bussinessItemData(vo);
            for (Map<String, Object> stringObjectMap : itemList) {
                Map tmpMap = new HashMap();
                tmpMap.put("name",StringUtils.getString(stringObjectMap.get("itemName")));
                tmpMap.put("value","");
                tmpMap.put("isOverStandard","");
                itemDataList.add(tmpMap);
            }
            map.put("itemDataList",itemDataList);
            map.put("overstandardList",overstandardList);
        }
        return map;
    }

    @Override
    public List<Map> qualityLevelDistribution(String ids, String startTime, String endTime, String controlLevelCode) {
        List<Map> resList = new ArrayList<>();
        SysDictData dictParam = new SysDictData();
        dictParam.setDictType("water_quality_level");
        List<SysDictData> sysDictDataList = sysDictDataService.selectDictDataList(dictParam);

        Date startDate = null;
        if(StringUtils.isNotEmpty(startTime)){
            startDate = DateUtils.dateTime("yyy-MM-dd",startTime);
        }
        Date endDate = null;
        if(StringUtils.isNotEmpty(endTime)){
            endDate = DateUtils.dateTime("yyy-MM-dd",endTime);
        }
        List<String> idList = new ArrayList<>();
        if(!StringUtils.isEmpty(ids)){
            idList = Arrays.asList(ids.split(","));
        }
        List<Map> list = busWaterIntegrationevaluatedataMapper.qualityLevelDistribution(idList,startDate,endDate,controlLevelCode);
        for (SysDictData sysDictData : sysDictDataList) {
            Map resMap = new HashMap();
            resMap.put("name",sysDictData.getDictLabel());
            int value = 0;
            if(!CollectionUtils.isEmpty(list)){
                for (Map map : list) {
                    String name = StringUtils.getString(map.get("name"));
                    if(sysDictData.getDictLabel().equals(name)){
                        value = Integer.valueOf(StringUtils.getString(map.get("value")));
                        break;
                    }
                }
            }
            resMap.put("value",value);
            resList.add(resMap);
        }
        return resList;
    }

    @Override
    public Map yearQualityLevelDistribution(String ids,String controlLevelCode) {
        Map resMap = new LinkedHashMap();
        SysDictData dictParam = new SysDictData();
        dictParam.setDictType("water_quality_level");
        List<SysDictData> sysDictDataList = sysDictDataService.selectDictDataList(dictParam);
        sysDictDataList.sort(Comparator.comparing(SysDictData::getDictSort));
        List<String> series = new ArrayList<>();
        for (SysDictData sysDictData : sysDictDataList) {
            series.add(sysDictData.getDictLabel());
        }
        resMap.put("series",series);

        List<String> xAxis = new ArrayList<>();

        List<List<Integer>> rawData = new ArrayList<>();

        Date endDate = DateUtils.getNowDate();

        Date startDate = DateUtils.addMonths(endDate,-12);
        startDate = DateUtils.dateTime("yyyy-MM-dd",DateUtils.getFirstDayOfMonth(startDate));

        List<String> idList = new ArrayList<>();
        if(!StringUtils.isEmpty(ids)){
            idList = Arrays.asList(ids.split(","));
        }
        Map<String,List<Map>> monthListMap = new LinkedHashMap<>();
        for(int i=0;i<12;i++){
            String month = DateUtils.parseDateToStr("yyyy-MM",DateUtils.addMonths(startDate,i));
            xAxis.add(month);

            Date startTime = DateUtils.dateTime("yyy-MM-dd",month+"-01");

            Date endTime = DateUtils.dateTime("yyy-MM-dd",month+"-31");

            List<Map> list = busWaterIntegrationevaluatedataMapper.qualityLevelDistribution(idList,startTime,endTime,controlLevelCode);
            monthListMap.put(month,list);
        }

        for (SysDictData sysDictData : sysDictDataList) {
            List<Integer> list = new ArrayList<>();
            for (String xAxi : xAxis) {
                int count = 0;
                List<Map> dataList = monthListMap.get(xAxi);
                if(!CollectionUtils.isEmpty(dataList)){
                    for (Map map : dataList) {
                        String name = StringUtils.getString(map.get("name"));
                        String value = StringUtils.getString(map.get("value"));
                        if(name.equals(sysDictData.getDictLabel())){
                            count = Integer.valueOf(value);
                        }
                    }
                }
                list.add(count);
            }
            rawData.add(list);
        }
        resMap.put("xAxis",xAxis);
        resMap.put("rawData",rawData);
        return resMap;
    }

    @Override
    public List<Map> standardDistribution(String ids, String startTime, String endTime, String controlLevelCode) {
        List<Map> resList = new ArrayList<>();

        List<SysDictData> sysDictDataList = new ArrayList<>();
        SysDictData sysDictDataDb = new SysDictData();
        sysDictDataDb.setDictLabel("达标");
        sysDictDataDb.setDictValue("0");
        sysDictDataList.add(sysDictDataDb);

        SysDictData sysDictDataCb = new SysDictData();
        sysDictDataCb.setDictLabel("超标");
        sysDictDataCb.setDictValue("1");
        sysDictDataList.add(sysDictDataCb);


        Date startDate = null;
        if(StringUtils.isNotEmpty(startTime)){
            startDate = DateUtils.dateTime("yyy-MM-dd",startTime);
        }
        Date endDate = null;
        if(StringUtils.isNotEmpty(endTime)){
            endDate = DateUtils.dateTime("yyy-MM-dd",endTime);
        }

        List<String> idList = new ArrayList<>();
        if(!StringUtils.isEmpty(ids)){
            idList = Arrays.asList(ids.split(","));
        }
        List<Map> list = busWaterIntegrationevaluatedataMapper.standardDistribution(idList,startDate,endDate,controlLevelCode);
        for (SysDictData sysDictData : sysDictDataList) {
            Map resMap = new HashMap();
            resMap.put("name",sysDictData.getDictLabel());
            int value = 0;
            if(!CollectionUtils.isEmpty(list)){
                for (Map map : list) {
                    String name = StringUtils.getString(map.get("name"));
                    if(sysDictData.getDictValue().equals(name)){
                        value = Integer.valueOf(StringUtils.getString(map.get("value")));
                        break;
                    }
                }
            }
            resMap.put("value",value);
            resList.add(resMap);
        }
        return resList;
    }

    @Override
    public List<Map> qualityLevelList(String ids, String startTime, String endTime, String controlLevelCode) {
        List<Map> resList = new ArrayList<>();

        Date startDate = null;
        if(StringUtils.isNotEmpty(startTime)){
            startDate = DateUtils.dateTime("yyy-MM-dd",startTime);
        }
        Date endDate = null;
        if(StringUtils.isNotEmpty(endTime)){
            endDate = DateUtils.dateTime("yyy-MM-dd",endTime);
        }

        List<String> idList = new ArrayList<>();
        if(!StringUtils.isEmpty(ids)){
            idList = Arrays.asList(ids.split(","));
        }
        BusWaterIntegrationevaluatedata param = new BusWaterIntegrationevaluatedata();
        param.setSegmentIdList(idList);
        param.setStartTime(startDate);
        param.setEndTime(endDate);
        param.setControlLevelCode(controlLevelCode);
        param.setBussinesscategorycode("CBN");
        List<BusWaterIntegrationevaluatedata> list = busWaterIntegrationevaluatedataMapper.selectBusWaterIntegrationevaluatedataList(param);
        if(!CollectionUtils.isEmpty(list)){
            for (BusWaterIntegrationevaluatedata busWaterIntegrationevaluatedata : list) {
                Map map = new HashMap();
                map.put("segmentname",busWaterIntegrationevaluatedata.getSegmentname());
                map.put("dataTime",DateUtils.parseDateToStr("yyyy-MM",busWaterIntegrationevaluatedata.getSampledate()));
                map.put("qualitylevelname",busWaterIntegrationevaluatedata.getQualitylevelname());
                resList.add(map);
            }
        }
        return resList;
    }

    @Override
    public Map statisCalendarDetail(String id, String month) {
        Map resMap = new HashMap();

        List<Map> dataMapList = new ArrayList<>();
        Date startDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",month+"-01 00:00:00");
        Date endDate = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",month+"-31 23:59:59");

        BusWaterIntegrationevaluatedata monthDataParam = new BusWaterIntegrationevaluatedata();
        monthDataParam.setSegmentid(id);
        monthDataParam.setStartTime(startDate);
        monthDataParam.setEndTime(endDate);
        List<BusWaterIntegrationevaluatedata> monthdataList = busWaterIntegrationevaluatedataMapper.selectBusWaterIntegrationevaluatedataList(monthDataParam);
        if(!CollectionUtils.isEmpty(monthdataList)){
            BusWaterIntegrationevaluatedata monthdata = monthdataList.get(0);
            resMap.put("qualitylevelname",monthdata.getQualitylevelname());
            resMap.put("segmentindex",monthdata.getSegmentindex());
        }else{
            resMap.put("qualitylevelname","");
            resMap.put("segmentindex","");
        }

        BusWaterIntegrationmonitordata param = new BusWaterIntegrationmonitordata();
        param.setSegmentid(id);
        param.setStartTime(startDate);
        param.setEndTime(endDate);
        List<BusWaterIntegrationmonitordata> dataList = busWaterIntegrationmonitordataMapper.selectBusWaterIntegrationmonitordataList(param);

        BussinessItemVo vo = new BussinessItemVo();
        vo.setBussinessCategoryCode("MSV");
        List<Map<String,Object>> itemList=publicToolsService.bussinessItemData(vo);
        for (Map<String, Object> itemMap : itemList) {
            String itemName = StringUtils.getString(itemMap.get("itemName"));
            String effectiveValue = "";
            String qualityLevelName = "";
            if(!CollectionUtils.isEmpty(dataList)){
                for (BusWaterIntegrationmonitordata busWaterIntegrationmonitordata : dataList) {
                    if(itemName.equals(busWaterIntegrationmonitordata.getItemname())){
                        if(busWaterIntegrationmonitordata.getEffectivevalue() != null){
                            effectiveValue = String.valueOf(busWaterIntegrationmonitordata.getEffectivevalue());
                        }
                        qualityLevelName = busWaterIntegrationmonitordata.getQualitylevelname();
                        break;
                    }
                }
            }
            Map dataMap = new HashMap();
            dataMap.put("itemName",itemName);
            dataMap.put("effectiveValue",effectiveValue);
            dataMap.put("qualityLevelName",qualityLevelName);
            dataMapList.add(dataMap);
        }
        resMap.put("dataList",dataMapList);

        return resMap;
    }

    @Override
    public Map excellentThreeInferiorFiveSum(String ids, String startTime, String endTime, String controlLevelCode) {

        Map resMap = new LinkedHashMap();
        Date startDate = null;
        if(StringUtils.isNotEmpty(startTime)){
            startDate = DateUtils.dateTime("yyy-MM-dd",startTime+"-01");
        }
        Date endDate = null;
        if(StringUtils.isNotEmpty(endTime)){
            endDate = DateUtils.dateTime("yyy-MM-dd",endTime+"-30");
        }
        List<String> idList = new ArrayList<>();
        if(!StringUtils.isEmpty(ids)){
            idList = Arrays.asList(ids.split(","));
        }
        List<String> controlLevelCodeList = new ArrayList<>();
        if(StringUtils.isNotEmpty(controlLevelCode)){
            controlLevelCodeList.addAll(Arrays.asList(controlLevelCode.split(",")));
        }
        //超标污染物
        List<Map> cbItemList = busWaterIntegrationmonitordataMapper.cbItemList(idList,startDate,endDate,controlLevelCodeList);

        //优三占比
        List<Map> excellentThreeList =  busWaterIntegrationmonitordataMapper.excellentThree(idList,startDate,endDate,controlLevelCodeList);

        //劣五
        List<Map> inferiorFiveList = busWaterIntegrationmonitordataMapper.inferiorFive(idList,startDate,endDate,controlLevelCodeList);

        List<String> dataTimeList = new ArrayList<>();
        List<String> excellentThreeCountList = new ArrayList<>();
        List<String> excellentThreeProporList = new ArrayList<>();
        List<String> inferiorFiveCountList = new ArrayList<>();
        List<String> inferiorFiveProporList = new ArrayList<>();

        while(!startDate.after(endDate)){
            String month = DateUtils.parseDateToStr("yyyy-MM",startDate);
            dataTimeList.add(month);

            float excellentThreeCount = 0f;
            float inferiorFiveCount = 0f;
            if(!CollectionUtils.isEmpty(excellentThreeList)){
                for (Map excellentThreeMap : excellentThreeList) {
                    String dataMonth = StringUtils.getString(excellentThreeMap.get("dataMonth"));
                    if(dataMonth.equals(month)){
                        excellentThreeCount = Float.valueOf(StringUtils.getString(excellentThreeMap.get("dataCount")));
                        break;
                    }
                }
            }

            if(!CollectionUtils.isEmpty(inferiorFiveList)){
                for (Map inferiorFiveMap : inferiorFiveList) {
                    String dataMonth = StringUtils.getString(inferiorFiveMap.get("dataMonth"));
                    if(dataMonth.equals(month)){
                        inferiorFiveCount = Float.valueOf(StringUtils.getString(inferiorFiveMap.get("dataCount")));
                        break;
                    }
                }
            }
            float count = excellentThreeCount + inferiorFiveCount;
            if(count == 0){
                excellentThreeCountList.add("0");
                excellentThreeProporList.add("0");
                inferiorFiveCountList.add("0");
                inferiorFiveProporList.add("0");
            }else{
                excellentThreeCountList.add(excellentThreeCount+"");
                excellentThreeProporList.add(String.format("%.2f",(excellentThreeCount/count)));
                inferiorFiveCountList.add(inferiorFiveCount+"");
                inferiorFiveProporList.add(String.format("%.2f",(inferiorFiveCount/count)));
            }
            startDate = DateUtils.addMonths(startDate,1);
        }

        List<String> excellentThreeListTop3 = new ArrayList<>();
        if(!CollectionUtils.isEmpty(excellentThreeList)){
            int count =3;
            if(excellentThreeList.size()<3){
                count = excellentThreeList.size();
            }
            for(int i=0;i<count;i++){
                excellentThreeListTop3.add(StringUtils.getString(excellentThreeList.get(i).get("dataMonth")));
            }
        }
        resMap.put("excellentThreeListTop3",excellentThreeListTop3);

        List<String> inferiorFiveListTop3 = new ArrayList<>();
        if(!CollectionUtils.isEmpty(inferiorFiveList)){
            int count = 3;
            if(inferiorFiveList.size()<3){
                count = inferiorFiveList.size();
            }
            for(int i=0;i<count;i++){
                inferiorFiveListTop3.add(StringUtils.getString(inferiorFiveList.get(i).get("dataMonth")));
            }
        }
        resMap.put("inferiorFiveListTop3",inferiorFiveListTop3);

        List<String> cbItemNameList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(cbItemList)){
            for (Map map : cbItemList) {
                cbItemNameList.add(StringUtils.getString(map.get("itemName")));
            }
        }
        resMap.put("cbItemList",cbItemNameList);

        resMap.put("dataTimeList",dataTimeList);
        resMap.put("excellentThreeCountList",excellentThreeCountList);
        resMap.put("excellentThreeProporList",excellentThreeProporList);
        resMap.put("inferiorFiveCountList",inferiorFiveCountList);
        resMap.put("inferiorFiveProporList",inferiorFiveProporList);

        return resMap;
    }
}
